<?php
namespace app\model;

use \core\lib\database;

class fileModel{
	private $db;
	
	public function __construct(){
		$this->db = new database();
	}
	
	public function addType($typename, $userid){
		$sql = "insert into filetype(name, userid) values('{$typename}', {$userid})";
		$ok = $this->db->update($sql);
		if($ok){
			return $this->db->insert_id();
		}
		return $ok;
	}
	
	public function loadType($userid){
		$sql = "select id, name from filetype where userid={$userid}";
		$ans = $this->db->query($sql);
		//p($sql);
		return $ans;
	}
	
	public function addFile($name, $info, $compressinfo, $typeid, $userid){
		$sql = "insert into fileinfo(name, info, compressinfo, typeid, userid) ";
		$sql .= "values('{$name}', '{$info}', '{$compressinfo}',{$typeid}, {$userid})";
		$ok = $this->db->update($sql);
		return $ok;
	}
	
	public function deleteFile($id){
		$sql = "delete from fileinfo where id = {$id}";
		$ok = $this->db->update($sql);
		return $ok;
	}
	
	public function updateFile($data, $compressdata, $id){
		$sql = "update `fileinfo` set `info`= ";
		$sql .= "'{$data}', `compressinfo`= ";
		$sql .= "'{$compressdata}' where id= ";
		$sql .= "{$id}";
		$ok = $this->db->update($sql);
		return $ok;
	}
	
	public function loadFileById($id){
		$sql = "select * from fileinfo where id = {$id}";
		$ans = $this->db->query($sql);
		return $ans[0];
	}
	
	public function loadFileByUser($userid){
		$sql = "select * from fileinfo where userid = {$userid} order by createtime desc";
		$ans = $this->db->query($sql);
		return $ans;
	}
	
	public function loadFileByName($name){
		$sql = "select * from fileinfo where name like '%{$name}%' order by createtime desc";
		$ans = $this->db->query($sql);
		return $ans;
	}
	
	public function loadFileByType($type){
		$sql = "select * from fileinfo where typeid = {$type} order by createtime desc";
		$ans = $this->db->query($sql);
		return $ans;
	}
	
}